use cfps2010adult_201906.dta, clear
* The individual-level data were obtained from the China Family Panel Studies 2010 baseline survey (CFPS, 2010). Although we are not permitted to share the data, it is available for free at: http://www.isss.pku.edu.cn/cfps/download/login.

merge m:1 qa102acode using birth_province_provincial_variables_merge_to_individuals
drop _merge 

// clean data
rename (qm509 qa1age cfps2010eduy_best) (collectivism age eduy)
replace qa102acode=. if qa102acode<0
replace collectivism=. if collectivism<0
replace eduy=. if eduy<0
replace income=. if income<0
g lincome=ln(income+1)

drop if qa102acode==.|eduy==.|lincome==.|collectivism==. 
drop if AgriculturalRiskCovered==.


********************************************************************************
********************************************************************************
********************************************************************************

global xi "age gender edu lincome"
global xr "latitude longitude area lgdp_per80"


//Table 1 & Supplementary Table 2
mixed collectivism  AgriculturalRiskCovered $xi $xr || qa102acode:, r cluster(qa102acode)
mixed collectivism  AgriculturalRiskAffected $xi $xr || qa102acode:, r cluster(qa102acode)


// Table 2
reg collectivism Disaster1470_1900 $xi $xr, r cluster(qa102acode)
ivreg2 collectivism $xi $xr (AgriculturalRiskCovered=Disaster1470_1900)  ,r cluster(qa102acode) first	
ivreg2 collectivism $xi $xr (AgriculturalRiskAffected=Disaster1470_1900)  ,r cluster(qa102acode) first




//source data for Fig.1
collapse (mean) collectivism AgriculturalRiskCovered AgriculturalRiskAffected, by (qa102acode)


//Fig.2
collapse (mean)  collectivism AgriculturalRiskCovered AgriculturalRiskAffected, by (qa102acode)
scatter  collectivism AgriculturalRiskCovered, mlabel(qa102acode) || lfit collectivism AgriculturalRiskCovered
scatter  collectivism AgriculturalRiskAffected, mlabel(qa102acode) || lfit collectivism AgriculturalRiskAffected

//Supplementary Fig.1
collapse (mean)  collectivism AgriculturalRiskCovered AgriculturalRiskAffected latitude longitude area, by (qa102acode)
reg collectivism latitude longitude area, r cluster(qa102acode) 
predict collectivism_r, residuals
reg AgriculturalRiskCovered latitude longitude area, r cluster(qa102acode) 
predict AgriculturalRiskCovered_r, residuals
reg AgriculturalRiskAffected latitude longitude area, r cluster(qa102acode) 
predict AgriculturalRiskAffected_r, residuals

scatter  collectivism_r AgriculturalRiskCovered_r, mlabel(qa102acode) || lfit collectivism_r AgriculturalRiskCovered_r
scatter  collectivism_r AgriculturalRiskAffected_r, mlabel(qa102acode) || lfit collectivism_r AgriculturalRiskAffected_r


//Part one of Supplementary Table 1
sum collectivism age gender edu lincome
duplicates drop qa102acode, force
sum AgriculturalRiskCovered AgriculturalRiskAffected latitude longitude   area  lgdp_per80 


//Supplementary Table 5

pwcorr qm503 collectivism, sig
pwcorr qm506 collectivism, sig

replace qm702=. if qm702<0
replace qm702=. if qm702==6
replace qm702=. if qm702<0
replace qm702=1000 if qm702==5
replace qm702=qm702+1 if qm702==3|qm702==4
replace qm702=3 if qm702==1000
pwcorr qm702 collectivism, sig

pwcorr qm502 collectivism, sig
pwcorr qm505 collectivism, sig


//Supplementary Table 6
collapse (mean)  collectivism latitude temperature AgriculturalSuitabilityPre1500 ProvincePercentHerder2000Sqrt RiceFarming lgdp_per80 Confucianism, by (qa102acode)

pwcorr latitude collectivism, sig
pwcorr temperature collectivism, sig
pwcorr AgriculturalSuitabilityPre1500 collectivism, sig
pwcorr ProvincePercentHerder2000Sqrt collectivism, sig
pwcorr RiceFarming collectivism, sig
pwcorr lgdp_per80 collectivism, sig
pwcorr Confucianism collectivism, sig


//Supplementary Table 7
forvalues i=1971(1)1990{
mixed collectivism  AgriculturalRiskCovered_End`i' $xi $xr || qa102acode:, r cluster(qa102acode)
mixed collectivism  AgriculturalRiskAffected_End`i' $xi $xr || qa102acode:, r cluster(qa102acode)
}


//Supplementary Table 8
mixed collectivism  AgriculturalRiskCovered AgriculturalRiskCovered_2009 $xi $xr || qa102acode:, r cluster(qa102acode)
mixed collectivism  AgriculturalRiskCovered AgriculturalRiskCovered_2010 $xi $xr || qa102acode:, r cluster(qa102acode)
mixed collectivism  AgriculturalRiskAffected AgriculturalRiskAffected_2009 $xi $xr || qa102acode:, r cluster(qa102acode)
mixed collectivism  AgriculturalRiskAffected AgriculturalRiskAffected_2010 $xi $xr || qa102acode:, r cluster(qa102acode)

//Supplementary Table 9
merge m:1 provcd using living_province_provincial_variables_merge_to_individuals
drop if _merge==2
drop _merge 

mixed collectivism  AgriculturalRiskCovered_living  $xi latitude_living longitude_living area_living  lgdp_per80_living if provcd==qa102acode|| provcd:, r cluster(provcd)
mixed collectivism  AgriculturalRiskAffected_living  $xi latitude_living longitude_living area_living lgdp_per80_living if provcd==qa102acode||provcd:, r cluster(provcd)
mixed collectivism  AgriculturalRiskCovered_living  $xi latitude_living longitude_living area_living lgdp_per80_living if provcd!=qa102acode|| provcd:, r cluster(provcd)
mixed collectivism  AgriculturalRiskAffected_living  $xi latitude_living longitude_living area_living lgdp_per80_living if provcd!=qa102acode||provcd:, r cluster(provcd)


//Supplementary Table 11
mixed collectivism AgriculturalRiskCovered BirthRate $xi $xr || qa102acode:, r cluster(qa102acode)
mixed collectivism  AgriculturalRiskAffected BirthRate $xi $xr || qa102acode:, r cluster(qa102acode)
mixed collectivism AgriculturalRiskCovered SexRatioAtBirth $xi $xr || qa102acode:, r cluster(qa102acode)
mixed collectivism  AgriculturalRiskAffected SexRatioAtBirth $xi $xr || qa102acode:, r cluster(qa102acode)


//Supplementary Table 12
mixed collectivism AgriculturalRiskCovered RiceFarming lgdp_per80 AverageMorbidityHumanTransOnly20 $xi $xr || qa102acode:, r cluster(qa102acode)
mixed collectivism  AgriculturalRiskAffected RiceFarming  lgdp_per80 AverageMorbidityHumanTransOnly20 $xi $xr || qa102acode:, r cluster(qa102acode)


//Supplementary Table 13
mixed collectivism  AgriculturalRiskCovered CultiLandPer80 $xi $xr || qa102acode:, r cluster(qa102acode)
mixed collectivism  AgriculturalRiskAffected CultiLandPer80 $xi $xr || qa102acode:, r cluster(qa102acode)
mixed collectivism  AgriculturalRiskCovered AgriculturalSuitabilityPre1500 $xi $xr || qa102acode:, r cluster(qa102acode)
mixed collectivism  AgriculturalRiskAffected AgriculturalSuitabilityPre1500 $xi $xr || qa102acode:, r cluster(qa102acode)

//Supplementary Table 14
mixed collectivism  AgriculturalRiskCovered $xi $xr if NonHanProvince==0&qa5code==1 || qa102acode:, r cluster(qa102acode)
mixed collectivism  AgriculturalRiskAffected $xi $xr if NonHanProvince==0&qa5code==1 || qa102acode:, r cluster(qa102acode)
mixed collectivism  AgriculturalRiskCovered ProvincePercentHerder2000Sqrt $xi $xr || qa102acode:, r cluster(qa102acode)
mixed collectivism  AgriculturalRiskAffected ProvincePercentHerder2000Sqrt $xi $xr || qa102acode:, r cluster(qa102acode)

// Supplementary Table 15
mixed collectivism  AgriculturalRiskCovered urban $xi $xr || qa102acode:, r cluster(qa102acode)
mixed collectivism  AgriculturalRiskAffected urban $xi $xr || qa102acode:, r cluster(qa102acode)
mixed collectivism  AgriculturalRiskCovered PopulationDensity80 $xi $xr || qa102acode:, r cluster(qa102acode)
mixed collectivism  AgriculturalRiskAffected PopulationDensity80 $xi $xr || qa102acode:, r cluster(qa102acode)
mixed collectivism AgriculturalRiskCovered PopulationDensity18thCentury $xi $xr || qa102acode:, r cluster(qa102acode)
mixed collectivism AgriculturalRiskAffected PopulationDensity18thCentury $xi $xr || qa102acode:, r cluster(qa102acode)

// Supplementary Table 16
mixed collectivism  AgriculturalRiskCovered QingRebellion $xi $xr || qa102acode:, r cluster(qa102acode)
mixed collectivism  AgriculturalRiskAffected QingRebellion $xi $xr || qa102acode:, r cluster(qa102acode)
mixed collectivism  AgriculturalRiskCovered QingWarfare $xi $xr || qa102acode:, r cluster(qa102acode)
mixed collectivism  AgriculturalRiskAffected QingWarfare $xi $xr || qa102acode:, r cluster(qa102acode)

//Supplementary Table 17
mixed collectivism  AgriculturalRiskCoveredEvent $xi $xr || qa102acode:, r cluster(qa102acode)
mixed collectivism  AgriculturalRiskAffectedEvent $xi $xr || qa102acode:, r cluster(qa102acode)


//Supplementary Table 18
mixed collectivism  AgriculturalRiskCovered Confucianism $xi $xr || qa102acode:, r cluster(qa102acode)
mixed collectivism  AgriculturalRiskAffected Confucianism $xi $xr || qa102acode:, r cluster(qa102acode)


//Supplementary Table 19
local c=1.3
reg collectivism AgriculturalRiskCovered $xi $xr , r cluster(qa102acode)
local r=e(r2)*1.3
psacalc beta AgriculturalRiskCovered, delta(1) rmax(`r') 
psacalc delta AgriculturalRiskCovered, beta(0) rmax(`r') 

local c=1.3
reg collectivism AgriculturalRiskAffected $xi $xr , r cluster(qa102acode)
local r=e(r2)*1.3
psacalc beta AgriculturalRiskAffected, delta(1) rmax(`r') 
psacalc delta AgriculturalRiskAffected, beta(0) rmax(`r') 

//Supplementary Table 20
reg collectivism  AgriculturalRiskCovered $xi $xr , r cluster(qa102acode) 
g constant=1
g cutoff1=4
g cutoff2=4
x_ols latitude longitude cutoff1 cutoff2 collectivism constant AgriculturalRiskCovered $xi $xr ,  xreg(10) coord(2)  

reg collectivism  AgriculturalRiskAffected $xi $xr , r cluster(qa102acode) 
g constant=1
g cutoff1=4
g cutoff2=4
x_ols latitude longitude cutoff1 cutoff2 collectivism constant AgriculturalRiskAffected $xi $xr , xreg(10) coord(2)

//Supplementary Discussion about Moran's I 
collapse (mean)   collectivism , by (qa102acode)
spatwmat using matrix_distance.dta, n(w)
*matrix_distance refers to the inverse of the geodesic distance between provinces
spatgsa collectivism, weight(w) moran twotail
